package com.opencloud.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.common.security.http.XRestTemplate;
import com.opencloud.system.client.model.entity.SystemApi;
import com.opencloud.system.client.service.ISystemApiServiceClient;
import com.opencloud.system.service.SystemApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liuyadu
 */
@Api(tags = "系统接口信息管理")
@RestController
public class SystemApiController implements ISystemApiServiceClient {
    @Autowired
    private SystemApiService apiService;
    @Autowired
    private XRestTemplate xRestTemplate;

    @ApiOperation(value = "获取接口列表", notes = "获取接口列表")
    @GetMapping(value = "/api")
    @Override
    public Result<Page<SystemApi>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(apiService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取接口列表", notes = "获取接口列表")
    @GetMapping("/api/list")
    @Override
    public Result<List<SystemApi>> getList(@RequestParam("serviceId") String serviceId) {
        return Result.ok().data(apiService.findList(serviceId));
    }

    @ApiOperation(value = "获取接口信息", notes = "获取接口信息")
    @GetMapping("/api/info")
    @Override
    public Result<SystemApi> get(@RequestParam("apiId") Long apiId) {
        return Result.ok().data(apiService.getById(apiId));
    }

    @ApiOperation(value = "添加/修改接口信息", notes = "添加/修改接口信息")
    @PostMapping("/api/save")
    @Override
    public Result<Long> save(@RequestBody SystemApi systemApi) {
        if (systemApi.getApiId() == null) {
            apiService.add(systemApi);
        } else {
            apiService.update(systemApi);
        }
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "移除接口信息", notes = "移除接口信息")
    @PostMapping("/api/remove")
    @Override
    public Result remove(@RequestParam("apiId") Long apiId) {
        apiService.remove(apiId);
        // 刷新网关
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "批量删除数据", notes = "批量删除数据")
    @PostMapping("/api/batch/remove")
    @Override
    public Result batchRemove(@RequestParam(value = "ids") String ids) {
        QueryWrapper<SystemApi> wrapper = new QueryWrapper();
        wrapper.lambda().in(SystemApi::getApiId, ids.split(",")).eq(SystemApi::getIsPersist, 0);
        apiService.remove(wrapper);
        // 刷新网关
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "批量修改", notes = "批量修改")
    @PostMapping("/api/batch/update")
    @Override
    public Result batchUpdate(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "value") Integer value,
            @RequestParam(value = "type") String type
    ) {
        QueryWrapper<SystemApi> wrapper = new QueryWrapper();
        wrapper.lambda().in(SystemApi::getApiId, ids.split(","));
        SystemApi entity = new SystemApi();
        if ("open".equals(type)) {
            entity.setIsOpen(value);
        } else if ("status".equals(type)) {
            entity.setStatus(value);
        } else if ("auth".equals(type)) {
            entity.setIsAuth(value);
        } else {
            return Result.ok();
        }
        apiService.update(entity, wrapper);
        // 刷新网关
        xRestTemplate.refreshGateway();
        return Result.ok();
    }
}
